package com.controller;

import com.entity.AddressBook;
import com.github.pagehelper.PageInfo;
import com.service.AddressBookService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;
import javax.annotation.Resource;
import java.util.List;

/**
 * (AddressBook)表控制层
 *
 * @author makejava
 * @since 2023-05-31 17:51:53
 */
@RestController
@RequestMapping("/doAddressBook")
public class AddressBookController {
    /**
     * 服务对象
     */
    @Resource
    private AddressBookService addressBookService;

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return 查询结果
     */
    @GetMapping("/byPage")
    public ResponseEntity<PageInfo<AddressBook>> queryByPage(@RequestParam(defaultValue = "1") int pageNum,
                                                         @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<AddressBook> pageInfo = addressBookService.selectByPage(pageNum, pageSize);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/id/{id}")
    public ResponseEntity<AddressBook> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.addressBookService.queryById(id));
    }

    /**
     * 分页查询,依据标签分别查询不同分组的数据
     *
     * @param pageNum
     * @param pageSize
     * @return 查询结果
     */
    @GetMapping("/label/{label}")
    public ResponseEntity<PageInfo<AddressBook>> queryPageByLabel(@RequestParam(defaultValue = "1") int pageNum,
                                                                  @RequestParam(defaultValue = "10") int pageSize
            , @PathVariable("label") String label) {
        PageInfo<AddressBook> pageInfo = addressBookService.labelByPage(pageNum,pageSize,label);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 新增数据
     *
     * @param addressBook 实体
     * @return 新增结果
     */
    @PostMapping("/addNewContact")
    public RedirectView add(AddressBook addressBook) {
        AddressBook insertedContact = this.addressBookService.insert(addressBook);
        if (insertedContact != null) {
            return new RedirectView("/addressBook");
        } else {
            return new  RedirectView("/addNewError");
        }
    }

    /**
     * 分页查询,依据标签分别查询不同分组的数据
     *
     * @param pageNum
     * @param pageSize
     * @return 查询结果
     */
    @GetMapping("/fuzzyQuery/{condition}")
    public ResponseEntity<PageInfo<AddressBook>> queryFuzzyPage(@RequestParam(defaultValue = "1") int pageNum,
                                                                  @RequestParam(defaultValue = "10") int pageSize
            , @PathVariable("condition") String condition) {
        PageInfo<AddressBook> pageInfo = addressBookService.fuzzyQuery(pageNum,pageSize,condition);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 编辑数据
     *
     * @param addressBook 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public RedirectView edit(AddressBook addressBook) {
        AddressBook updateContact = this.addressBookService.update(addressBook);

        if (updateContact != null) {
            return new RedirectView("/addressBook");
        } else {
            return new RedirectView("/updateError");
        }
    }

    /**
     * 删除数据至回收站
     *
     * @param selectedContacts 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> deleteById(@RequestBody List<Integer> selectedContacts) {
        return ResponseEntity.ok(this.addressBookService.deleteById(selectedContacts));
    }

    /**
     * 彻底删除数据
     *
     * @param selectedContacts 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/trueDelete")
    public ResponseEntity<Boolean> trueDeleteById(@RequestBody List<Integer> selectedContacts) {
        return ResponseEntity.ok(this.addressBookService.trueDeleteById(selectedContacts));
    }

//    回收站分页显示
    @GetMapping("/recyclePage")
    public ResponseEntity<PageInfo<AddressBook>> recyclePage(@RequestParam(defaultValue = "1") int pageNum,
                                                             @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<AddressBook> pageInfo = addressBookService.recyclePage(pageNum, pageSize);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 从回收站恢复数据
     * @param selectedContacts 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/recover")
    public ResponseEntity<Boolean> recover(@RequestBody List<Integer> selectedContacts) {
        return ResponseEntity.ok(this.addressBookService.recover(selectedContacts));
    }

}

